        var identityProviders = [];
        var cookieName = "ACSChosenIdentityProvider";
        var cookieExpiration = 30; // days
        var maxImageWidth = 240;
        var maxImageHeight = 40;
 
        // This function will be called back by the HRD metadata, and is responsible for displaying the sign-in page.
        function ShowSigninPage(json) {
            var cookieName = GetHRDCookieName();
            var numIdentityProviderButtons = 0;
            var showMoreOptionsLink = false;
            identityProviders = json;

            // Loop through the identity providers
            for (var i in identityProviders) {
                // Show all sign-in options if no cookie is set
                if (cookieName === null) {
                    CreateIdentityProviderButton(identityProviders[i]);
                    numIdentityProviderButtons++;
                }
                // Show only the last selected identity provider if a cookie is set
                else {
                    if (cookieName == identityProviders[i].Name) {
                        CreateIdentityProviderButton(identityProviders[i]);
                        numIdentityProviderButtons++;
                    }
                    else {
                        showMoreOptionsLink = true;
                    }
                }
            }
            //If the user had a cookie but it didn\'t match any current identity providers, show all sign-in options 
            if (cookieName !== null && numIdentityProviderButtons === 0) {
                ShowDefaultSigninPage();
            }
            //Othewise, render the sign-in page normally
            else {
                ShowSigninControls(numIdentityProviderButtons, showMoreOptionsLink);
            }
            document.getElementById("Main").style.display = "";
        }
 
        // Resets the sign-in page to its original state before the user logged in and received a cookie.
        function ShowDefaultSigninPage() {
            var numIdentityProviderButtons = 0;
            document.getElementById("IdentityProvidersList").innerHTML = "";
            for (var i in identityProviders) {
                CreateIdentityProviderButton(identityProviders[i]);
                numIdentityProviderButtons++;
            }
            ShowSigninControls(numIdentityProviderButtons, false);
        }
 
        //Reveals the sign-in controls on the sign-in page, and ensures they are sized correctly
        function ShowSigninControls(numIdentityProviderButtons, showMoreOptionsLink) {
 
            //Display the identity provider links, and size the page accordingly
            if (numIdentityProviderButtons > 0) {
                document.getElementById("LeftArea").style.display = "";
            }
            //Show a link to redisplay all sign-in options
            if (showMoreOptionsLink) {
                document.getElementById("MoreOptions").style.display = "";
            }
            else {
                document.getElementById("MoreOptions").style.display = "none";
            }
        }
 
		//Creates a stylized link to an identity provider\'s login page
		function CreateIdentityProviderButton(identityProvider) {	
			var idpList = document.getElementById("IdentityProvidersList");
			var button = document.createElement("button");
			button.setAttribute("name", identityProvider.Name);
			button.setAttribute("id", identityProvider.LoginUrl );
			button.className = "IdentityProvider";
			button.onclick = IdentityProviderButtonClicked;
				
			// Display an image if an image URL is present
			if (identityProvider.ImageUrl.length > 0) {
				
				var img = document.createElement("img");
				img.className = "IdentityProviderImage";
				img.setAttribute("src", identityProvider.ImageUrl);
				img.setAttribute("alt", identityProvider.Name);
				img.setAttribute("border", "0");
				
				// If the image is larger than the button, scale maintaining aspect ratio.
		        if (img.height > maxImageHeight || img.width > maxImageWidth) {
		            var resizeRatio = 1;
		            if( img.width/img.height > maxImageWidth/maxImageHeight )
		            {
		                // Aspect ratio wider than the button
		                resizeRatio = maxImageWidth / img.width;
			        }
			        else
			        {
			            // Aspect ratio taller than or equal to the button
			            resizeRatio = maxImageHeight / img.height;
			        }
			        
                    img.setAttribute("height", img.height * resizeRatio);
			        img.setAttribute("width", img.width * resizeRatio);
		        }
				button.appendChild(img);
			}
			// Otherwise, display a text link if no image URL is present
			else if (identityProvider.ImageUrl.length === 0) {
				
				button.appendChild(document.createTextNode(identityProvider.Name));
			}
			idpList.appendChild(button);	
		}
 
        // Gets the name of the remembered identity provider in the cookie, or null if there isn\'t one.
        function GetHRDCookieName() {
            var cookie = document.cookie;
            if (cookie.length > 0) {
                var cookieStart = cookie.indexOf(cookieName + "=");
                if (cookieStart >= 0) {
                    cookieStart += cookieName.length + 1;
                    var cookieEnd = cookie.indexOf(";", cookieStart);
                    if (cookieEnd == -1) {
                        cookieEnd = cookie.length;
                    }
                    return unescape(cookie.substring(cookieStart, cookieEnd));
                }
            }
            return null;
        }
 
        // Sets a cookie with a given name
        function SetCookie(name) {
            var expiration = new Date();
            expiration.setDate(expiration.getDate() + cookieExpiration);
            document.cookie = cookieName + "=" + escape(name) + ";expires=" + expiration.toUTCString();
        }
 
        // Sets a cookie to remember the chosen identity provider and navigates to it.
        function IdentityProviderButtonClicked() {
	        if (window.event && window.event.keyCode == 13) return false;
            SetCookie(this.getAttribute("name"));
            window.location = this.getAttribute("id");
            return false;
        }

    